
package multicastqosrouting;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import org.jgraph.graph.DefaultEdge;
import org.jgrapht.GraphPath;
import org.jgrapht.ListenableGraph;
import org.jgrapht.alg.KShortestPaths;

/**
 *
 * @author yeison
 */
public class RoutingTable {

    /*
     * Constructor
     */
    public RoutingTable(ListenableGraph graph) {
        g = graph;
    }
    /*
     * Getters
     */

    public ArrayList<ArrayList<Integer>> getRoutingTable() {
        return routingTable;
    }

    public void getNumberOfPathsFound() {
        System.out.println("The number of paths found to the node " + i_destinationNode + " is: " + listKAlgorithm.size());
    }
    /*
     * Method: findKShortesPaths
     */
    /*
     * Finds the KShortest Path using the Kshortest algorithm from JGrapthT
     * http://jgrapht.org/javadoc/org/jgrapht/alg/KShortestPaths.html It uses a
     * Bellman-Ford's variation
     */

    public void findKShortestPaths(int sourceNode, int destinationNode, int numberOfPaths) {
        i_sourceNode = sourceNode;
        i_destinationNode = destinationNode;
        i_numberOfPaths = numberOfPaths;
        //System.out.println(g.toString());
        kAlgorithm = new KShortestPaths<Long, DefaultEdge>(g, (long) i_sourceNode, i_numberOfPaths);
        listKAlgorithm = kAlgorithm.getPaths((long) i_destinationNode);
        populateRoutingTable();
    }

    /*
     * Method: populateRoutingTable
     */
    /*
     * Extract from the KShortest output the number of the nodes to build the
     * Routing table Example: 1 2 2 3 3 4
     */
    private void populateRoutingTable() {
        routingTable = new ArrayList<ArrayList<Integer>>();
        String subPath;
        for (GraphPath<Long, DefaultEdge> pathString : listKAlgorithm) {
            routingTable.add(new ArrayList<Integer>());
            subPath = pathString.toString();
            StringTokenizer num = new StringTokenizer(subPath, ":(),[] ");
            while (num.hasMoreTokens()) {
                String digits = num.nextToken();
                routingTable.get(listKAlgorithm.indexOf(pathString)).add(Integer.parseInt(digits.trim()));
            }
//  for (int i = 0; i < subPath.length(); i++) {
//  char c = subPath.charAt(i);
//  String stringCharacter = ""+c;
//  if (Character.isDigit(c)) routingTable.get(listKAlgorithm.indexOf(pathString)).add(Integer.parseInt(stringCharacter)); 				
//  }			
        }
    }

    public void printRoutingTable() {

        System.out.println("The routing table to the node " + i_destinationNode + " generated by the program: ");

        for (int i = 0; i < routingTable.size(); i++) {
            for (int j = 0; j < routingTable.get(i).size(); j++) {
                System.out.print(routingTable.get(i).get(j) + " ");
            }
            System.out.println();

        }
    }

    public void printKShortestPath() {
        System.out.println("KShortestPath to Node " + i_destinationNode + " = " + listKAlgorithm.toString());
    }
//instance variables
    private ListenableGraph g;
    private int i_sourceNode;
    private int i_destinationNode;
    private int i_numberOfPaths;
    private KShortestPaths<Long, DefaultEdge> kAlgorithm;
    private List<GraphPath<Long, DefaultEdge>> listKAlgorithm;
    private ArrayList<ArrayList<Integer>> routingTable;
}
